package cn.tyf.controller;

import cn.tyf.domain.PageBean;
import cn.tyf.domain.ResultInfo;
import cn.tyf.domain.WxFun;
import cn.tyf.service.WxFunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.RequestWrapper;
import java.io.*;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/wxfun")
public class WxFunController {

    @Autowired
    private WxFunService wxFunService;

    @GetMapping("/findFunAll")
    @ResponseBody
    public List<WxFun> wxFunList() {
        return wxFunService.wxFunList();
    }

    @GetMapping("/findFunPage")
    @ResponseBody
    public ResultInfo findFunPage(Integer currentPage, Integer pageSize) {
        ResultInfo info = new ResultInfo();
        PageBean wxFunPage = wxFunService.findWxFunPage(currentPage, pageSize);
        if (wxFunPage != null) {
            info.setFlag(true);
            info.setData(wxFunPage);
            info.setType(false);
        }
        return info;
    }

    @PostMapping("/addWxfun")
    public String addWxFun(@RequestParam("fid") Integer fid,
                           @RequestParam("drive") String drive,
                           @RequestParam("description") String description,
                           @RequestParam("img") MultipartFile file,
                           HttpServletRequest request) {

        WxFun flag = wxFunService.findByFid(fid);
        if (flag == null) {
            //获取文件名称
            String filename = file.getOriginalFilename();
            //判断文件名是否为空
            if ("".equals(filename)) {
                return "redirect:/home.jsp";
            }
            //设置上传的文件的保存位置
            String separator = File.separator;
            String path = request.getSession().getServletContext().getRealPath(separator) + separator + "wximg";
            //判断路径是否存在，不存在则创建
            File realPath = new File(path);
            if (!realPath.exists()) {
                realPath.mkdir();
            }
            try {
                WxFun wxFun = new WxFun(null,fid, "wximg/" + filename, drive, description, 1);
                wxFunService.addFun(wxFun);
                InputStream is = file.getInputStream();
                OutputStream os = new FileOutputStream(new File(path, filename));
                byte[] bytes = new byte[1024];
                while (is.read(bytes) != -1) {
                    os.write(bytes);
                }
                os.flush();
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "redirect:/home.jsp";
    }


    @GetMapping("/findbyFid")
    @ResponseBody
    public WxFun findbyFid(Integer fid) {
        return wxFunService.findByFid(fid);
    }

    @GetMapping("/delWxfun")
    @ResponseBody
    public boolean delWxfun(int id) {
        wxFunService.delWxfun(id);
        return true;
    }

    @PostMapping("/updateWxFun")
    @ResponseBody
    public boolean updateWxFun(@RequestParam("id") Integer id,
                              @RequestParam("fid") String fid,
                              @RequestParam("drive") String drive,
                              @RequestParam("description") String description,
                              @RequestParam("img") MultipartFile file,
                              HttpServletRequest request) {

        //获取文件名称
        String filename = file.getOriginalFilename();
        //设置上传的文件的保存位置
        String separator = File.separator;
        String path = request.getSession().getServletContext().getRealPath(separator) + separator + "wximg";
        //判断路径是否存在，不存在则创建
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        try {
            WxFun wxFun = new WxFun(id,Integer.parseInt(fid), "wximg/" + filename, drive, description, 1);
            wxFunService.updateWxFun(wxFun);
            InputStream is = file.getInputStream();
            OutputStream os = new FileOutputStream(new File(path, filename));
            byte[] bytes = new byte[1024];
            while (is.read(bytes) != -1) {
                os.write(bytes);
            }
            os.flush();
            is.close();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    @PostMapping("/updateWxFunUimg")
    @ResponseBody
    public boolean updateWxFun(@RequestParam("id") Integer id,
                              @RequestParam("fid") String fid,
                              @RequestParam("drive") String drive,
                              @RequestParam("description") String description) {
        WxFun wxFun = new WxFun(id,Integer.parseInt(fid),drive,description);
        wxFunService.updateWxFun(wxFun);
        return true;
    }


    @GetMapping("/deleteByIds")
    @ResponseBody
    public int deleteByIds(String[] checkbox){
        return wxFunService.delWxfunByIds(Arrays.asList(checkbox));
    }

}
